package com.vcs.domain;

import java.util.regex.*;
import java.util.*;

public class Utils {

	public static boolean isValidEmailAddress(String aEmailAddress) {
		// Checks for email addresses starting with
		// inappropriate symbols like dots or @ signs.
		Pattern p = Pattern.compile("^\\.|^\\@");
		Matcher m = p.matcher(aEmailAddress);
		if (m.find())
			return false;
		// Checks for email addresses that start with
		// www. and prints a message if it does.
		p = Pattern.compile("^www\\.");
		m = p.matcher(aEmailAddress);
		if (m.find()) {
			return false;
		}
		p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
		m = p.matcher(aEmailAddress);
		StringBuffer sb = new StringBuffer();
		boolean result = m.find();
		boolean deletedIllegalChars = false;

		while (result) {
			deletedIllegalChars = true;
			m.appendReplacement(sb, "");
			result = m.find();
		}

		// Add the last segment of input to the new String
		m.appendTail(sb);

		aEmailAddress = sb.toString();

		if (deletedIllegalChars) {
			return false;
		}
		return true;
	}

}
